﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OT.Data;
using OT.Areas.Admin.Models;
using PagedList;
namespace OT.Areas.Admin.Controllers
{
    public class LanguageController : Controller
    {
        #region Declaration
        readonly OTEntitiesme OTDb = new OTEntitiesme();
        #endregion

        #region Index
        public ActionResult Index(string searchString, string sortOrder, string currentFilter, int? page, string filedName)
        {
            int pageSize = 0, pageNumber = 0;
            var Language = (List<Languages>)null;
            try
            {
                ViewBag.CurrentSort = sortOrder;
                if (filedName == "LanguageName")
                    ViewBag.NameSortParm = sortOrder == "LanguageName" ? "LanguageName_desc" : "LanguageName";
                if (searchString != null)
                    page = 1;
                else
                    searchString = currentFilter;
                ViewBag.CurrentFilter = searchString;
                Language = (from c in OTDb.Languages
                        select new Languages
                        {
                            LanguageID = c.LanguageID,
                            LanguageCode = c.LanguageCode,
                            LanguageName = c.LanguageName,
                            Description=c.Description
                           
                        }).ToList();
                if (!String.IsNullOrEmpty(searchString))
                    Language = Language.Where(r => r.LanguageName.Contains(searchString)).ToList();
                switch (sortOrder)
                {
                    case "LanguageName_desc":
                        Language = Language.OrderByDescending(r => r.LanguageName).ToList();
                        break;
                    case "LanguageName":
                        Language = Language.OrderBy(r => r.LanguageName).ToList();
                        break;
                   
                    default:
                        Language = Language.OrderBy(r => r.LanguageName).ToList();
                        break;
                }
                pageSize = 10;
                pageNumber = (page ?? 1);
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return View(Language.ToPagedList(pageNumber, pageSize));
        }
#endregion

        #region Create
        public ActionResult Create()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Create(Languages Languag)
        {
            Language Lng = null;
            try
            {
                if (ModelState.IsValid)
                {
                    Lng = new Language();
                    Lng.LanguageCode = Languag.LanguageCode;
                    Lng.LanguageName = Languag.LanguageName;
                    Lng.Description = Languag.Description;
                    OTDb.Languages.Add(Lng);
                    OTDb.SaveChanges(); 
                    TempData["notice"] = "Successfully registered";
                    return RedirectToAction("Index");
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return View(Languag);
        }
        #endregion

        #region Edit
        public ActionResult Edit(int LanguageID)
        {
            Languages Language = null;
            try
            {
                var Lang = OTDb.Languages.Where(r => r.LanguageID == LanguageID).SingleOrDefault();
                if (Lang != null)
                {
                    Language = new Languages();
                    Language.LanguageCode = Lang.LanguageCode;
                    Language.LanguageName = Lang.LanguageName;
                    Language.Description = Lang.Description;
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return View(Language);
        }
        [HttpPost]
        public ActionResult Edit(int LanguageID, Languages Language)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var category = OTDb.Languages.Where(r => r.LanguageID == LanguageID).SingleOrDefault();
                    if (category != null)
                    {
                        category.LanguageCode = Language.LanguageCode;
                        category.LanguageName = Language.LanguageName;
                        category.Description = Language.Description;
                        OTDb.SaveChanges();
                        TempData["notice"] = "Successfully Update";
                        return RedirectToAction("Index");
                    }
                }
                else
                {
               
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return View(Language);
        }
        #endregion

        #region Delete
        public ActionResult Delete(int LanguageID)
        {
            try
            {
                var Language = OTDb.Languages.Where(r => r.LanguageID == LanguageID).SingleOrDefault();
                if (Language != null)
                {
                    OTDb.Languages.Remove(Language);
                    OTDb.SaveChanges();
                    TempData["notice"] = "Successfully Delete";
                    return RedirectToAction("Index");
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return View();
        }
        #endregion
    }
}
